﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.DishModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.DishModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;

namespace Resto.CostControl.Core.Facade.DishModule.FacadeObject
{
    /// <summary>
    /// 菜肴FO
    /// </summary>
    public class DishFO : IApplicationFacadeObject
    {
        #region 私有属性

        private DishDQO _DishDQO;
        private DishDQO DishDQO
        {
            get
            {
                if (_DishDQO == null)
                    _DishDQO = ObjectCreator.Create<DishDQO>();

                return _DishDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddDish(DishDTO dishDTO, List<DishMaterialDTO> items)
        {
            Dish dish = dishDTO.ToDish();
            CreateDishMaterial(dish, items);

            dish.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            dishDTO.ID = dish.ID;
        }

        public virtual void UpdateDish(DishDTO dishDTO, List<DishMaterialDTO> items)
        {
            Dish dish = DishDQO.GetDish(dishDTO.ID);
            CreateDishMaterial(dish, items);
            dishDTO.CopyToDish(dish);

            RestoCostDomainSession.CurrentSession.SaveChanges();

            DishDTOCache.Remove(dish.ID);
        }

        public virtual void DeleteDish(string id)
        {
            Dish dish = DishDQO.GetDish(id);

            dish.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            DishDTOCache.Remove(dish.ID);
        }

        public virtual DishDTO GetDishDTO(string id)
        {
            Dish dish = DishDQO.GetDish(id);
            if (dish.IsDelete)
                return null;

            return new DishDTO(dish);
        }

        /// <summary>
        /// 获取菜单配比明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DishMaterialDTO[] GetDishMaterialDTOArray(string id)
        {
            Dish dish = DishDQO.GetDish(id);
            if (dish.IsDelete)
                return new DishMaterialDTO[0];

            List<DishMaterialDTO> list = new List<DishMaterialDTO>();
            foreach (DishMaterial item in dish.DishMaterials)
            {
                DishMaterialDTO itemDTO = new DishMaterialDTO(item.MaterialID);
                itemDTO.Weight = item.Weight;

                list.Add(itemDTO);
            }

            return list.ToArray();
        }

        #endregion

        #region 私有方法

        private void CreateDishMaterial(Dish dish, List<DishMaterialDTO> items)
        {
            dish.ClearMaterial();

            foreach (DishMaterialDTO itemDTO in items)
            {
                DishMaterial item = itemDTO.ToDishMaterial();
                dish.AddMaterial(item);
            }
        }

        #endregion
    }
}
